#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>


char *wbuf = "Hello World\n";
char rbuf[128];

int main(void)
{
	printf("EmbedCharDev test\n");

	//dev_0写入数据
	int fd_0 = open("/dev/chrdev_0", O_RDWR);
	if (-1 == fd_0) {
		perror("open0");
		return -1;
	}
	write(fd_0, wbuf, strlen(wbuf));
	write(fd_0, "this is dev_0\n", strlen("this is dev_0\n"));
	close(fd_0);
	//dev_0读取文件内容
	fd_0 = open("/dev/chrdev_0", O_RDWR);
	read(fd_0, rbuf, 128);
	printf("The dev_0 content : [%s]\n", rbuf);
	close(fd_0);

	//dev_1写入数据
	int fd_1 = open("/dev/chrdev_1", O_RDWR);
	write(fd_1, wbuf, strlen(wbuf));
	write(fd_1, "this is dev_1\n", strlen("this is dev_1\n"));
	close(fd_1);
	//dev_1读取文件内容
	fd_1 = open("/dev/chrdev_1", O_RDWR);
	if (-1 == fd_1) {
		perror("open1");
		return -1;
	}
	read(fd_1, rbuf, 128);
	printf("The dev_1 content : [%s]\n", rbuf);
	close(fd_1);

	return 0;
}

//1. cat /proc/devices                 : 查看设备号<dev_id>
//2. mknod /dev/chrdev_0 c <dev_id> 0    : 手动创建设备文件 /dev/chrdev_0
//3. 运行 ./chrdev_test 测试
//4. rm /dev/chrdev                    ：卸载删除
